<template>
  <div class="club-detail-container">
    <div v-if="club" class="club-detail">
      <div class="club-header">
        <img :src="club.logo || '/default-club.png'" alt="社团标志">
        <div class="club-info">
          <h1>{{ club.name }}</h1>
          <p class="club-category">{{ club.category }}</p>
          <p class="club-president">社长: {{ club.president.username }}</p>
          <p class="club-members">{{ club.members }} 成员</p>
          <button class="join-btn" v-if="!isMember">加入社团</button>
          <button class="joined-btn" v-else disabled>已加入</button>
        </div>
      </div>
      <div class="club-description">
        <h2>社团介绍</h2>
        <p>{{ club.description }}</p>
      </div>
      <div class="club-activities">
        <h2>社团活动</h2>
        <div class="activities-list">
          <div v-for="activity in clubActivities" :key="activity.id" class="activity-item" @click="goToActivityDetail(activity.id)">
            <div class="activity-info">
              <h3>{{ activity.title }}</h3>
              <p class="activity-time">{{ formatDate(activity.start_time) }}</p>
              <p class="activity-location">{{ activity.location }}</p>
              <div class="activity-status" :class="[(activity.current_participants >= activity.max_participants && activity.status === 'registration_open') ? 'full' : activity.status]">
                {{ (activity.current_participants >= activity.max_participants && activity.status === 'registration_open') ? '已报满' : statusMap[activity.status] }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-else class="loading">加载中...</div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { activityApi } from '@/api'
import { formatDate } from '@/utils/date'

const route = useRoute()
const router = useRouter()
const clubId = route.params.id
const club = ref(null)
const clubActivities = ref([])
const isMember = ref(false)

// 状态映射
const statusMap = {
  'not_started': '未开始报名',
  'registration_open': '报名中',
  'ongoing': '进行中',
  'completed': '已结束',
  'cancelled': '已取消'
}

// 获取社团详情
const fetchClubDetail = async () => {
  try {
    const res = await activityApi.getClubDetail(clubId)
    club.value = res.data
    // 检查是否为社团成员
    isMember.value = res.data.is_member || false
  } catch (error) {
    console.error('获取社团详情失败:', error)
  }
}

// 获取社团活动
const fetchClubActivities = async () => {
  try {
    const params = {
      club: clubId
    }
    const res = await activityApi.getActivities(params)
    clubActivities.value = res.data.results
  } catch (error) {
    console.error('获取社团活动失败:', error)
  }
}

// 加入社团
const joinClub = async () => {
  try {
    // 这里假设有关联API，实际根据后端接口调整
    // await activityApi.joinClub(clubId)
    // 暂时模拟加入状态
    isMember.value = true
    club.value.members += 1
  } catch (error) {
    console.error('加入社团失败:', error)
  }
}

// 跳转到活动详情
const goToActivityDetail = (activityId) => {
  router.push({ name: 'activityDetail', params: { id: activityId } })
}

// 页面加载时执行
onMounted(() => {
  fetchClubDetail()
  fetchClubActivities()
})
</script>

<style scoped>
.club-detail-container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
}

.club-header {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.club-header img {
  width: 150px;
  height: 150px;
  object-fit: cover;
  border-radius: 50%;
}

.club-info {
  flex: 1;
}

.club-info h1 {
  font-size: 24px;
  margin-bottom: 10px;
}

.club-category,
.club-president,
.club-members {
  color: #666;
  margin-bottom: 8px;
}

.join-btn,
.joined-btn {
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 10px;
}

.joined-btn {
  background-color: #e0e0e0;
  cursor: not-allowed;
}

.club-description,
.club-activities {
  margin-bottom: 30px;
}

.club-description h2,
.club-activities h2 {
  font-size: 20px;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid #eee;
}

.activities-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}

.activity-item {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 15px;
  cursor: pointer;
  transition: box-shadow 0.3s;
  min-height: 160px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.activity-item:hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* 移除了列表中的图片显示 */

.activity-status.full {
  background-color: #fef0f0;
  color: #e6a23c;
}

.activity-info {
  padding: 12px;
}

.activity-info h3 {
  font-size: 16px;
  margin-bottom: 8px;
}

.activity-time,
.activity-location {
  color: #999;
  font-size: 12px;
  margin-bottom: 4px;
}

.activity-status {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  margin-top: 8px;
}

.activity-status.not_started {
  background-color: #f0f9eb;
  color: #67c23a;
}

.activity-status.ongoing {
  background-color: #ecf5ff;
  color: #4096ff;
}

.activity-status.ended {
  background-color: #f5f5f5;
  color: #909399;
}

.activity-status.cancelled {
  background-color: #fef0f0;
  color: #f56c6c;
}

.loading {
  text-align: center;
  padding: 40px;
  color: #666;
}
  .activity-status {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
  }

  .activity-status.not_started {
    background-color: #f0f9eb;
    color: #67c23a;
  }

  .activity-status.registration_open {
    background-color: #e6f7ff;
    color: #1890ff;
  }

  .activity-status.ongoing {
    background-color: #ecf5ff;
    color: #4096ff;
  }

  .activity-status.completed {
    background-color: #f5f5f5;
    color: #909399;
  }

  .activity-status.cancelled {
    background-color: #fef0f0;
    color: #f56c6c;
  }
</style>